﻿(function($){
	$.fn.xmlediter = function(args) {	
	    var _this=$(this);
	    if(_this.attr("xmleditered")){
	        return;
	    }else{
	        _this.attr("xmleditered","true")
	    }
	    var val=_this.val();
	    _this.hide();
	    var xml=$(val);
	    if($.browser.msie&&val!=""){
            xml = new ActiveXObject("Microsoft.XMLDOM");
            xml.async = false;
            xml.loadXML(val);
            xml=$(xml);
           }
         //else{
//            xml = new DOMParser().parseFromString(val, "text/xml");
//        }

        

	    var rows=xml.find("item");

	    //var colums=rows.eq(0).children();
	    var table=$("<table width='100%'>");
	    table.attr("id",_this.attr("id")+"_xmledter_table");
	    _this.parent().append(table);
	    
	    
	    var getitem=function(columuname){
	        var item=null;
	        for(var i=0;i<defaultcolums.length;i++){
	            if(defaultcolums[i].columuname==columuname){
	                item=defaultcolums[i];
	                break;
	            }
	        }
	        for(var i=0;i<rows.size();i++){
	            if(rows.eq(i).find("columuname").text()==columuname){
	                item={
	                    columuname:rows.eq(i).find("columuname").text(),
	                    titlename:rows.eq(i).find("titlename").text(),
	                    visible:rows.eq(i).find("visible").text(),
	                    controltype:rows.eq(i).find("controltype").text(),
	                    tag:rows.eq(i).find("tag").text(),
	                    //itemsid:rows.eq(i).find("itemsid").text(),
	                    //filetype:rows.eq(i).find("filetype").text(),
	                    description:rows.eq(i).find("description").text(),
	                    defaultvalue:rows.eq(i).find("defaultvalue").text(),
	                    listable:rows.eq(i).find("listable").text(),
	                    searchable:rows.eq(i).find("searchable").text(),
	                    isrequired:rows.eq(i).find("isrequired").text(),
	                    isunique:rows.eq(i).find("isunique").text()
	                };
	                break;
	            }
	        }
	        
	        return item;
	    }
	    
	    var updatevalue=function(){
	        var items=table.find("tr:not(:eq(0))");
	        var str="";
	        for(var i=0;i<items.size();i++){
	            item=items.eq(i);
	            str+="<item>\n";
	            str+="<columuname>"+item.find("td:eq(0)").text()+"</columuname>\n";
	            str+="<titlename>"+item.find("td:eq(1) input").val()+"</titlename>\n";
	            str+="<visible>"+(item.find("td:eq(2) input").attr("checked")?"1":"0")+"</visible>\n";
	            str+="<controltype>"+item.find("td:eq(3) select").val()+"</controltype>\n";
	            str+="<tag>"+item.find("td:eq(4) input").val()+"</tag>\n";
	            //str+="<itemsid>"+item.find("td:eq(5) input").val()+"</itemsid>\n";
	            //str+="<filetype>"+item.find("td:eq(6) input").val()+"</filetype>\n";
	            str+="<description>"+item.find("td:eq(5) input").val()+"</description>\n";
	            str+="<defaultvalue>"+item.find("td:eq(6) input").val()+"</defaultvalue>\n";
	            str+="<listable>"+(item.find("td:eq(7) input").attr("checked")?"1":"0")+"</listable>\n";
	            str+="<searchable>"+(item.find("td:eq(8) input").attr("checked")?"1":"0")+"</searchable>\n";
	            str+="<isrequired>"+(item.find("td:eq(9) input").attr("checked")?"1":"0")+"</isrequired>\n";
	            str+="<isunique>"+(item.find("td:eq(10) input").attr("checked")?"1":"0")+"</isunique>\n";
	            str+="</item>\n";
	        }
	        str="<config>"+str+"</config>"
	        _this.val(str);	        
	    };
	    
	    var defaultcolums=[	            {
	                columuname:"title",
	                titlename:"标题",
	                visible:"1",
	                controltype:"textbox",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"1",
	                searchable:"1",
	                isrequired:"0",
	                isunique:"0"
	            },
	            {
	                columuname:"subtitle",
	                titlename:"副标题",
	                visible:"0",
	                controltype:"textbox",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
	            {
	                columuname:"titlecolor",
	                titlename:"标题颜色",
	                visible:"0",
	                controltype:"color",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
	            {
	                columuname:"titleblod",
	                titlename:"标题是否加粗",
	                visible:"0",
	                controltype:"bit",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"0",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
	            {
	                columuname:"tag",
	                titlename:"标签",
	                visible:"1",
	                controltype:"textbox",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"用|或者,分割",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
	            {
	                columuname:"imagepath",
	                titlename:"图片",
	                visible:"1",
	                controltype:"upload",
	                tag:"",
	                //itemsid:0,
	                //filetype:"jpg,gif,png,bmp",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
	            {
	                columuname:"smallimagepath",
	                titlename:"小图片",
	                visible:"0",
	                controltype:"upload",
	                tag:"",
	                //itemsid:0,
	                //filetype:"jpg,gif,png,bmp",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
	            {
	                columuname:"info",
	                titlename:"信息内容",
	                visible:"1",
	                controltype:"htmlarea",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
                {
	                columuname:"source",
	                titlename:"信息来源",
	                visible:"1",
	                controltype:"textbox",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
                {
	                columuname:"author",
	                titlename:"作者",
	                visible:"1",
	                controltype:"textbox",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
                {
	                columuname:"hits",
	                titlename:"点击量",
	                visible:"1",
	                controltype:"readonly",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"1",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
                {
	                columuname:"addtime",
	                titlename:"发布时间",
	                visible:"1",
	                controltype:"datetime",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"1",
	                searchable:"1",
	                isrequired:"0",
	                isunique:"0"
	            },
                {
	                columuname:"ordertype",
	                titlename:"排序状态",
	                visible:"1",
	                controltype:"dropdownlist",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"0",
	                listable:"1",
	                searchable:"1",
	                isrequired:"0",
	                isunique:"0"
	            },
                {
	                columuname:"checkstate",
	                titlename:"checkstate",
	                visible:"0",
	                controltype:"dropdownlist",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
	            {
	                columuname:"pubuser",
	                titlename:"pubuser",
	                visible:"0",
	                controltype:"textbox",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
	            {
	                columuname:"pubtime",
	                titlename:"pubtime",
	                visible:"0",
	                controltype:"datetime",
	                tag:"",
	                ////itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
	            {
	                columuname:"checkuser",
	                titlename:"checkuser",
	                visible:"0",
	                controltype:"dropdownlist",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
                {
	                columuname:"checktime",
	                titlename:"checktime",
	                visible:"0",
	                controltype:"datetime",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
                {
	                columuname:"string_1",
	                titlename:"string_1",
	                visible:"0",
	                controltype:"textbox",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
	            {
	                columuname:"string_2",
	                titlename:"string_2",
	                visible:"0",
	                controltype:"textbox",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
	            {
	                columuname:"string_3",
	                titlename:"string_3",
	                visible:"0",
	                controltype:"textbox",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
	            {
	                columuname:"int_1",
	                titlename:"int_1",
	                visible:"0",
	                controltype:"dropdownlist",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
                {
	                columuname:"int_2",
	                titlename:"int_2",
	                visible:"0",
	                controltype:"dropdownlist",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
                {
	                columuname:"int_3",
	                titlename:"int_3",
	                visible:"0",
	                controltype:"dropdownlist",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
                {
	                columuname:"text_1",
	                titlename:"text_1",
	                visible:"0",
	                controltype:"textbox",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
                {
	                columuname:"text_2",
	                titlename:"text_2",
	                visible:"0",
	                controltype:"textbox",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            },
                {
	                columuname:"text_3",
	                titlename:"text_3",
	                visible:"0",
	                controltype:"textbox",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            }   ,
                {
	                columuname:"bit_1",
	                titlename:"bit_1",
	                visible:"0",
	                controltype:"bit",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            }   ,
                {
	                columuname:"bit_2",
	                titlename:"bit_2",
	                visible:"0",
	                controltype:"bit",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            }   ,
                {
	                columuname:"bit_3",
	                titlename:"bit_3",
	                visible:"0",
	                controltype:"bit",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            }   ,
                {
	                columuname:"lang",
	                titlename:"语言",
	                visible:"0",
	                controltype:"lang",
	                tag:"",
	                //itemsid:0,
	                //filetype:"",
	                description:"",
	                defaultvalue:"",
	                listable:"0",
	                searchable:"0",
	                isrequired:"0",
	                isunique:"0"
	            }   ];
	            
	    var tr=$("<tr>");
	    tr.appendTo(table);
	    $("<th>字段</th>").appendTo(tr);
	    $("<th>名称</th>").appendTo(tr);
	    $("<th>是否显示</th>").appendTo(tr);
	    $("<th>显示方式</th>").appendTo(tr);
	    $("<th>Tag</th>").appendTo(tr);
	    //$("<th>候选项ID</th>").appendTo(tr);
	    //$("<th>文件类型</th>").appendTo(tr);
	    $("<th>描述</th>").appendTo(tr);
	    $("<th>默认值</th>").appendTo(tr);
	    $("<th>是否显示在列表页</th>").appendTo(tr);
	    $("<th>是否可搜索</th>").appendTo(tr);
	    $("<th>必填项</th>").appendTo(tr);
	    $("<th>不可重复</th>").appendTo(tr);
	    
	    for(var i=0;i<defaultcolums.length;i++){
	        var tr=$("<tr>");
	        tr.appendTo(table);
	        var tdcolumuname=$("<td>"+defaultcolums[i].columuname+"</td>");	     
	        tdcolumuname.appendTo(tr);   
	        var item=getitem(defaultcolums[i].columuname);
	            if(item!=null){
	                //名称
	                var td=$("<td>");
	                td.appendTo(tr);
	                var control=$("<input  style='width:80px'  type='text' value='"+item.titlename+"'/>");
	                control.appendTo(td);
	                control.blur(function(){updatevalue()});
    	            
	                //是否显示
	                td=$("<td>");
	                td.appendTo(tr);
	                var control=$("<input type='checkbox' "+(item.visible=="1"||item.visible=="Y"||item.visible=="true"?"checked='checked'":"")+"/>");
	                control.appendTo(td);
	                control.click(function(){updatevalue()});
    	            
	                 //显示方式
	                td=$("<td>");
	                td.appendTo(tr);
	                var control=$("<select>");
	                $("<option "+(item.controltype=="textbox"?"selected='selected'":"")+" value='textbox'>输入框</option>").appendTo(control);
	                $("<option "+(item.controltype=="dropdownlist"?"selected='selected'":"")+" value='dropdownlist'>下拉列表</option>").appendTo(control);
	                $("<option "+(item.controltype=="radio"?"selected='selected'":"")+" value='radio'>单选按钮组</option>").appendTo(control);
	                $("<option "+(item.controltype=="classification"?"selected='selected'":"")+" value='classification'>分类选择</option>").appendTo(control);
	                $("<option "+(item.controltype=="datetime"?"selected='selected'":"")+" value='datetime'>日期</option>").appendTo(control);
	                $("<option "+(item.controltype=="readonly"?"selected='selected'":"")+" value='readonly'>只读</option>").appendTo(control);
	                $("<option "+(item.controltype=="bit"?"selected='selected'":"")+" value='bit'>复选框</option>").appendTo(control);
	                $("<option "+(item.controltype=="color"?"selected='selected'":"")+" value='color'>颜色</option>").appendTo(control);
	                $("<option "+(item.controltype=="singelimgupload"?"selected='selected'":"")+" value='singelimgupload'>单图片上传</option>").appendTo(control);
	                $("<option "+(item.controltype=="multiimgupload"?"selected='selected'":"")+" value='multiimgupload'>多图片上传</option>").appendTo(control);
	                $("<option "+(item.controltype=="singelfileupload"?"selected='selected'":"")+" value='singelfileupload'>单文件上传</option>").appendTo(control);
	                $("<option "+(item.controltype=="multifileupload"?"selected='selected'":"")+" value='multifileupload'>多文件上传</option>").appendTo(control);
	                $("<option "+(item.controltype=="htmlarea"?"selected='selected'":"")+" value='htmlarea'>编辑器</option>").appendTo(control);
	                $("<option "+(item.controltype=="textarea"?"selected='selected'":"")+" value='textarea'>多行文本</option>").appendTo(control);
	                $("<option "+(item.controltype=="lang"?"selected='selected'":"")+" value='lang'>语言</option>").appendTo(control);
	                //$("<option "+(item.controltype=="tabletitle"?"selected='selected'":"")+" value='tabletitle'>TableTitle</option>").appendTo(control);
	                control.appendTo(td);
	                control.change(function(){updatevalue()});
    	            
    	             //TAG
	                td=$("<td>");
	                td.appendTo(tr);
	                var control=$("<input style='width:200px' type='text' value='"+item.tag+"'/>");
	                control.appendTo(td);
	                control.blur(function(){updatevalue()});
	                
	                //候选项ID
//	                td=$("<td>");
//	                td.appendTo(tr);
//	                var control=$("<input style='width:30px' type='text' value='"+item.itemsid+"'/>");
//	                control.appendTo(td);
//	                control.blur(function(){updatevalue()});
//    	            
//	                //文件类型
//	                td=$("<td>");
//	                td.appendTo(tr);
//	                var control=$("<input  style='width:80px'  type='text' value='"+item.filetype+"'/>");
//	                control.appendTo(td);
//	                control.blur(function(){updatevalue()});
    	            
	                //描述
	                td=$("<td>");
	                td.appendTo(tr);
	                var control=$("<input type='text' value='"+item.description+"'/>");
	                control.appendTo(td);
	                control.blur(function(){updatevalue()});
    	            
	                //默认值
	                td=$("<td>");
	                td.appendTo(tr);
	                var control=$("<input type='text' value='"+item.defaultvalue+"'/>");
	                control.appendTo(td);
	                control.blur(function(){updatevalue()});
    	            
	                 //是否显示在列表页
	                td=$("<td>");
	                td.appendTo(tr);
	                var control=$("<input type='checkbox' "+(item.listable=="1"||item.listable=="Y"||item.listable=="true"?"checked='checked'":"")+"/>");
	                control.appendTo(td);
	                control.click(function(){updatevalue()});
    	            
	                 //是否可搜索
	                td=$("<td>");
	                td.appendTo(tr);
	                var control=$("<input type='checkbox' "+(item.searchable=="1"||item.searchable=="Y"||item.searchable=="true"?"checked='checked'":"")+"/>");
	                control.appendTo(td);
	                control.click(function(){updatevalue()});
    	            
	                 //必填项
	                td=$("<td>");
	                td.appendTo(tr);
	                var control=$("<input type='checkbox' "+(item.isrequired=="1"||item.isrequired=="Y"||item.isrequired=="true"?"checked='checked'":"")+"/>");
	                control.appendTo(td);
	                control.click(function(){updatevalue()});
    	            
	                 //不可重复
	                td=$("<td>");
	                td.appendTo(tr);
	                var control=$("<input type='checkbox' "+(item.isunique=="1"||item.isunique=="Y"||item.isunique=="true"?"checked='checked'":"")+"/>");
	                control.appendTo(td);
	                control.click(function(){updatevalue()});
    	            
	         }
	      
	     }
	     
	     updatevalue();
	    $(".DataDiv").width($(".DataDiv table:eq(0)").width());
    }
})(jQuery)
